package com.sw.study.springbootstudy.service.impl;

import com.sw.study.springbootstudy.mapper.ArticleMapper;
import com.sw.study.springbootstudy.pojo.Article;
import com.sw.study.springbootstudy.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author sw
 * @project springboot-study
 * @description
 * @date 2020/4/22 10:17
 */
@Service
public class IArticleServiceImpl   implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public List<Article> queryArticles() {
        return articleMapper.queryArticles();
    }

    @Override
    @Cacheable(cacheNames = "articles",unless = "#result==null")
    public Article selectById(Integer id) {
        return articleMapper.selectById(id);
    }

    @Override
    @CachePut(cacheNames = "articles",key = "#result.id")
    public Article upadteArticle(Article article) {
         articleMapper.upadteArticle(article);
         return article;
    }

    @Override
    @CacheEvict(cacheNames = "articles")
    public void deleteArticle(Integer id) {
         articleMapper.deleteArticle(id);

    }
}
